package com.welkom.everydate.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.welkom.everydate.common.Constants;
import com.welkom.everydate.dao.UserDao;
import com.welkom.everydate.domain.Menu;
import com.welkom.everydate.domain.User;
import com.welkom.everydate.services.interfaces.MenuService;
import com.welkom.everydate.services.interfaces.UserService;
@Controller
public class UserDetailsController {
	@Resource
	UserService userService;
	@Resource
	MenuService menuService;
	
	@RequestMapping("/index.html")
	public String loginSuccess(Model model) {
		String userName = null;
		User user = null;
		Object principal = SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();
		if (principal instanceof UserDetails) {
			userName = ((UserDetails) principal).getUsername();
		} else {
			userName = principal.toString();
		}
		if (userName != null) {
			user = userService.getUserByUserName(userName);
		}
		model.addAttribute("userDetail", user);
		return "common/index";
	}
	
	@RequestMapping("/userIndex.html")
	public String userLoginSuccess(Model model) {
		String userName = null;
		User user = null;
		Object principal = SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();
		if (principal instanceof UserDetails) {
			userName = ((UserDetails) principal).getUsername();
		} else {
			userName = principal.toString();
		}
		if (userName != null) {
			user = userService.getUserByUserName(userName);
		}
		model.addAttribute("userDetail", user);
		return "business/index";
	}
	
	 @RequestMapping("/userDetail.html")
	 @ResponseBody
	 public User getUserDetail(){
		 String userName = null;
			User user = null;
			Object principal = SecurityContextHolder.getContext()
					.getAuthentication().getPrincipal();
			if (principal instanceof UserDetails) {
				userName = ((UserDetails) principal).getUsername();
			} else {
				userName = principal.toString();
			}
			if (userName != null) {
				user = userService.getUserByUserName(userName);
			}
			return user;
	 }
	 
	 @RequestMapping("/getUserMenus.html")
	 @ResponseBody
	 public List<Menu> getUserMenus(){
		 String userName = null;
			User user = null;
			Object principal = SecurityContextHolder.getContext()
					.getAuthentication().getPrincipal();
			if (principal instanceof UserDetails) {
				userName = ((UserDetails) principal).getUsername();
			} else {
				userName = principal.toString();
			}
			List<Menu> allMenus=menuService.getMenusByUserName(userName);
			List<Menu> finalMenus=new ArrayList<Menu>();
			for(Menu menu:allMenus){
				if(menu.getMenuLevel()==Constants.MenuLevel.MENU_LEVEL_FIRST){
					menu.setSubMenus(new ArrayList<Menu>());
					finalMenus.add(menu);
				}
			}
			for(Menu menu:allMenus){
				if(menu.getMenuLevel()==Constants.MenuLevel.MENU_LEVEL_SECOND){
				for(Menu parentMenu:finalMenus){
					
					if(menu.getParentMenuId()==parentMenu.getMenuId()){
						parentMenu.getSubMenus().add(menu);
					}
				}
				}
			}
			return finalMenus;
	 }
	

}
